//
//  APIGenerator.m
//  CodeGenerator
//
//  Created by 丁玉松 on 2017/9/15.
//  Copyright © 2017年 Beijing Yingyan Internet Co., Ltd. All rights reserved.
//

#import "APIGenerator.h"
#import "DMGParameter.h"
#import "NSString+FileHandle.h"

@implementation APIGenerator


#pragma mark - AddInterface

-(void)yyi_writeAPIWithApiName:(NSString *)apiName url:(NSString *)url superCls:(NSString *)superCls parameterArray:(NSArray *)parameterArray toFilePath:(NSString *)filePath functionDesi:(NSString *)functionDesi isResponse:(BOOL)isResponse{
    
    NSString * markMacro = [NSString stringWithFormat:@"\n\n#pragma mark - %@\n\n",functionDesi];

    /************api接口***************/

    NSString *initMethod = [self yyi_apiInitMethodWithParameterArray:parameterArray];
    if (initMethod.length > 0) {
        initMethod = [NSString stringWithFormat:@"%@;",initMethod];
    }
    
    NSString *apiInterStr = [NSString stringWithFormat:
                             @"\
                             \n@interface %@%@ : %@\n\
                             \n%@\n\
                             \n@end\n",superCls,apiName,superCls,initMethod];
    
    NSString *hFile = [NSString stringWithFormat:@"%@%@",markMacro,apiInterStr];
    
    [hFile yyi_writeTofilePath:filePath anchor:nil];
    
    
    
    /************api实现***************/
    //生成宏
    NSString *urlMacro = [self yyi_generateMacroWithApiName:apiName url:url];

    NSString *initImpStr = [self yyi_apiInitMethodImpWithParameterArray:parameterArray];
    
    NSString *urlImpStr = [self yyi_apiImpRequestUrlMethod:apiName];
    
    NSString *modelImpStr = @"";
    if (isResponse) {
        modelImpStr = [self yyi_apiImpModelClassNameMethod:apiName];
    }
    
    NSString *impStr = [NSString stringWithFormat:
                         @"\
                         \n@implementation %@%@\
                         \n%@\n\
                         \n%@\n\
                         \n%@\n\
                         \n@end",superCls,apiName,initImpStr,urlImpStr,modelImpStr];

    NSString *mFile = [NSString stringWithFormat:@"%@%@%@",markMacro,urlMacro,impStr];
    NSString *ocm = [filePath stringByReplacingCharactersInRange:NSMakeRange(filePath.length-1, 1) withString:@"m"];
    [mFile yyi_writeTofilePath:ocm anchor:nil];
}





-(NSString *)yyi_apiInitMethodWithParameterArray:(NSArray *)parameterArray{
    if (parameterArray.count == 0) {
        return @"";
    }
    
    NSMutableString *str = [NSMutableString new];
    for (DMGParameter *pa in parameterArray) {
        [str appendString:[NSString stringWithFormat:@"%@:(%@*)%@ ",pa.name,pa.prop,pa.name]];
    }
    str = (NSMutableString *)[str stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[[str substringWithRange:NSMakeRange(0, 1)]uppercaseString]];
    [str replaceCharactersInRange:NSMakeRange(str.length-1, 1) withString:@""];
    
    NSString *initMethodStr = [NSString stringWithFormat:@"- (instancetype)initWith%@",str];
    return initMethodStr;
}


-(NSString *)yyi_generateMacroWithApiName:(NSString *)apiName url:(NSString *)url{
    NSString *urlMacro = [NSString stringWithFormat:@"#define kapi_%@  @\"%@\"\n",apiName,url];
    return urlMacro;
}


#pragma mark -

-(NSString *)yyi_apiInitMethodImpWithParameterArray:(NSArray *)parameterArray{
    if (parameterArray.count == 0) {
        return @"";
    }

    NSMutableString *aStr = [NSMutableString new];
    for (DMGParameter *pa in parameterArray) {
        [aStr appendString:[NSString stringWithFormat:
                            @"\
                            \n      [parameters setValue:%@    forKey:@\"%@\"];"
                            ,pa.name,pa.name]];
    }
    
    NSString *initMethod = [self yyi_apiInitMethodWithParameterArray:parameterArray];

    NSString *initStr = [NSString stringWithFormat:@"\
                         \n%@{\
                         \n  self = [super init];\
                         \n  if (self) {\
                         \n      NSMutableDictionary *parameters = [NSMutableDictionary dictionary];\
                         %@\
                         \n      self.parameters = parameters;\
                         \n  }\
                         \n  return self;\
                         \n}",initMethod,aStr];
    
    return initStr;
}


- (NSString *)yyi_apiImpRequestUrlMethod:(NSString *)apiName{
    NSString *str = [NSString stringWithFormat:
                     @"\
                     \n-(NSString *)requestUrl{\
                     \n    return kapi_%@;\
                     \n}\
                     ",apiName];
    return str;
}


- (NSString *)yyi_apiImpModelClassNameMethod:(NSString *)apiName{
    NSString *str = [NSString stringWithFormat:
                        @"\
                        \n-(NSString *)modelClassName{\
                        \n    return @\"DMG%@Model\";\
                        \n}\
                        ",apiName];
    return str;
}


@end
